package com.timothy.android;


import com.timothy.question.TypeBean2;
import com.timothy.question.TypeBean2Config;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class LayoutB extends BaseActivity{
	
	private static final String LOG_TAG = "LayoutB";
	
	private LinearLayout layoutB;
	private TextView tv_msg = null;
	private Button btnA = null;
	private Button btnB = null;
	private Button btnC = null;
	
	private Button btnPre = null;
	private Button btnNext = null;

	private Drawable btnDrawable;
	
	private TypeBean2 bean;
	private TypeBean2Config config;
//	private TextView timeTV;
	MyCount mc;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layoutb);
		
		//mock receive data
		bean = TypeBean2.getMockInstance();
		config = bean.getConfig();
		
		layoutB = (LinearLayout) this.findViewById(R.id.layoutB);
		layoutB.setBackgroundColor(Color.CYAN);
		
		tv_msg = (TextView) this.findViewById(R.id.promotMsg);
		tv_msg.setText(bean.getQuestion());
		tv_msg.setTextColor(config.getLabelTextColor());
		tv_msg.setTextSize(config.getLabelTextSize());
		tv_msg.setBackgroundColor(config.getLabelBgColor());
		
		btnA = (Button) this.findViewById(R.id.btnA);
		btnA.setText(bean.getChoice()[0]);
		btnA.setHeight(80);
		btnA.setTextColor(config.getButtonTextColor());
		btnA.setTextSize(config.getButtonTextSize());
		
		btnB = (Button) this.findViewById(R.id.btnB);
		btnB.setText(bean.getChoice()[1]);
		btnB.setHeight(80);
		btnB.setTextColor(config.getButtonTextColor());
		btnB.setTextSize(config.getButtonTextSize());
		
		btnC = (Button) this.findViewById(R.id.btnC);
		btnC.setText(bean.getChoice()[2]);
		btnC.setHeight(80);
		btnC.setTextColor(config.getButtonTextColor());
		btnC.setTextSize(config.getButtonTextSize());

        //得到button背景图片
        Resources resources = this.getResources(); 
        btnDrawable = resources.getDrawable(R.drawable.button);
        
		btnA.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				btnA.setBackgroundColor(Color.YELLOW);
				btnB.setBackgroundDrawable(btnDrawable); 
				btnC.setBackgroundDrawable(btnDrawable); 
			}
		});
		
		btnB.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				btnA.setBackgroundDrawable(btnDrawable);
				btnB.setBackgroundColor(Color.YELLOW); 
				btnC.setBackgroundDrawable(btnDrawable); 
			}
		});
		
		btnC.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				btnA.setBackgroundDrawable(btnDrawable);
				btnB.setBackgroundDrawable(btnDrawable); 
				btnC.setBackgroundColor(Color.YELLOW); 
			}
		});
		
		btnPre = (Button) this.findViewById(R.id.btnPre);
		btnPre.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
			    intent.setClass(LayoutB.this,LayoutA.class);
			    startActivity(intent);
			}
		});
		
		btnNext = (Button) this.findViewById(R.id.btnNext);
		btnNext.setEnabled(false);
		
//		//设置屏幕显示方向
//		if("L".equalsIgnoreCase(config.getOrientation())){
//			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);	
//		}else if("P".equalsIgnoreCase(config.getOrientation())){
//			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//		}
		
//		//超时处理
//		timeTV = (TextView) this.findViewById(R.id.timeTV);
//		timeTV.setTextSize(30);
//		timeTV.setTextColor(Color.parseColor("#CC00CC"));
		int time = config.getTimeLimit();
		int interval = config.getInterval();
        mc = new MyCount(time * 1000,interval * 1000);  
        mc.start();
        
        //屏幕保持亮光(始终不锁屏)
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        
        //设定锁屏时间
//        try {
//        	lockScreenTime = Settings.System.getInt(this.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT);
//		} catch (SettingNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//        Settings.System.putInt(this.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 10000);
	}
	
	//Activity 销毁时的操作
	protected void onDestroy() {
        super.onDestroy();
        
        //恢复手机原来锁屏时间
//        Settings.System.putInt(this.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, lockScreenTime);
        
        //首先回到桌面
        Intent i = new Intent(Intent.ACTION_MAIN);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.addCategory(Intent.CATEGORY_HOME);
        startActivity(i);      
    }
	
	//倒计时内部类
	class MyCount extends CountDownTimer {   
		public MyCount(long millisInFuture, long countDownInterval) {   
			super(millisInFuture, countDownInterval);   
		}   
		@Override   
		public void onFinish() {   
//			timeTV.setText("Time Over!"); 
//			btnA.setTextColor(Color.parseColor("#969696"));
			btnA.setEnabled(false);
//			btnB.setTextColor(Color.parseColor("#969696"));
			btnB.setEnabled(false);
//			btnC.setTextColor(Color.parseColor("#969696"));
			btnC.setEnabled(false);
		}   
		@Override   
		public void onTick(long millisUntilFinished) {   
//			timeTV.setText("Time Left:  " + millisUntilFinished / 1000 + " S ...");   
			Toast.makeText(LayoutB.this, millisUntilFinished / 1000 + "", Toast.LENGTH_SHORT).show();//toast有显示时间延迟 
		} 
		
/*		public void showToast(){
	    	LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    	View view=li.inflate(R.layout.countdown_toast,null);
	    	//把布局文件toast.xml转换成一个view
	    	Toast toast= new Toast();
	    	toast.setView(view);
	    	//载入view,即显示toast.xml的内容
	    	TextView tv=(TextView)view.findViewById(R.id.countDownTV);
	    	tv.setText("Toast View");
	    	//修改TextView里的内容
	    	toast.setDuration(Toast.LENGTH_SHORT);
	    	//设置显示时间，长时间Toast.LENGTH_LONG，短时间为Toast.LENGTH_SHORT,不可以自己编辑
	    	toast.show();
	    }*/
	}

	@Override
	protected void processMessage(int what, String content) {
		// TODO Auto-generated method stub
		
	}
	

}
